#pragma once 
#include<iostream>
#include<signal.h>
#include<pthread.h>
#include"TcpServer.hpp"
#include"ThreadPool.hpp"
#include"Task.hpp"
#define PORT 8081
class HttpServer
{
  private:
    int _port;
    bool stop;
  public:
    HttpServer(int port=PORT):_port(port),stop(false)
    {}
    void InitServer()
    {
      //服务器忽略sigpipe信号，防止客户端关闭服务器挂掉
      signal(SIGPIPE,SIG_IGN);
      //tcp_server=TcpServer::getinstance(_port);
    }
    void Loop()
    {
     TcpServer* tsvr=TcpServer::getinstance(_port);

      LOG(INFO,"Loop begin");
      while(!stop)
      {
        struct sockaddr_in peer;
        socklen_t len=sizeof(peer);
        int sock=accept(tsvr->Sock(),(struct sockaddr*)&peer,&len);
        if(sock<0)
        {
          continue;
        }
        LOG(INFO,"Get a new link"); 
        Task task(sock);
        ThreadPool::GetInstance()->PushTask(task);    
      }
    }
    
};
